package com.bigfreakindeals.Activities;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.List;

import org.apache.http.util.EncodingUtils;
import org.w3c.dom.Text;

import com.bigfreakindeals.R;
import com.bigfreakindeals.Application.ApplicationEx;
import com.bigfreakindeals.Common.Constant;
import com.bigfreakindeals.Common.Tools;
import com.bigfreakindeals.Models.ActiveDeal;
import com.bigfreakindeals.Models.UniversityDeals;


import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class CreditCardActivity extends BaseActivity implements OnClickListener{
	RelativeLayout mainpage;
	RelativeLayout maindeals;         //grayed area deals page
	RelativeLayout maindealsNoDeals; // grayed area no deals

	RelativeLayout thumbnailLayout;

	RelativeLayout terms;
	View header;
	ListAdapter adapter;
	ListView dealListView;
	Button headerButtonLeft;
	Button headerButtonRight;
	TextView beanSays;
	Button share;
	Button discuss;
	
	Animation contextMenuIn;
	Animation contextMenuOut;

	TextView timer;
	Button beanSaysNoDeals;
	Long remainingTimer = (long) 0;

	TextView headerTitle;
	Button tf_highLights;
	Button tf_finePrint;
	Button tf_company;
	LinearLayout headertopBG;

	TextView textviewQuantity;
	 TextView textviewAmount;
	 TextView textviewPricetv;
	
	LinearLayout headerbottomBG;
	RelativeLayout tabtext_bg;
	TextView tabtext;
	WebView appView;
	TextView noDealsErrorMessage;
	ActiveDeal active = new ActiveDeal();
	UniversityDeals universitydeals;
	boolean hasdeals = true;
	
	List<ActiveDeal> dealList;
	double amount=0.00,totalprice = 0.00;
	int quantity;
		
	String sessionID;
	String quantitypassed;
	String dealID;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getActivityView();
	}
	public void getActivityView(){
			
		//init buy page scree
		setContentView(R.layout.buycc);
		
	
		quantitypassed = getIntent().getStringExtra("QUANTITY");
		dealID = getIntent().getStringExtra("DEALID");
		sessionID =  getIntent().getStringExtra("SESSIONID");
	}
	
	public void getPaybyCreditcard(){
		
		((Button)findViewById(R.id.header_iconleft)).setVisibility(View.GONE);
		
		universitydeals = ApplicationEx.getInstance().myProfile.getlastSelectedSchool();
		String header = universitydeals.getSchoolTitle();
		
		((TextView)findViewById(R.id.header_title)).setText(header);
		
		headertopBG = (LinearLayout) findViewById(R.id.headerbg_top);
		headerbottomBG = (LinearLayout) findViewById(R.id.headerbg_bottom);
	
		if (universitydeals.getColor_titleBarBottomColor() > -1 && universitydeals.getColor_titleBarTopColor() > -1) {
			if (headerbottomBG != null) {
				headerbottomBG.setBackgroundColor(universitydeals.getColor_titleBarBottomColor() + (-16777216));
			}
			if (headertopBG != null)
				headertopBG.setBackgroundColor(universitydeals.getColor_titleBarTopColor() + (-16777216));
		}
		
		((ProgressBar) findViewById(R.id.progress)).setVisibility(View.VISIBLE);
		((Button)findViewById(R.id.header_iconright)).setVisibility(View.VISIBLE);
		((Button)findViewById(R.id.header_iconright)).setBackgroundResource(R.drawable.exit_btn);
		((Button)findViewById(R.id.header_iconright)).setOnClickListener(this);
		WebViewClient webViewClientExt = new WebViewClientExt();
		appView = (WebView) findViewById(R.id.webview);
		appView.setVisibility(View.VISIBLE);
		appView.setBackgroundColor(0);
		appView.setVerticalScrollbarOverlay(true);
		appView.setFocusableInTouchMode(true);
		appView.setFocusable(true);
		appView.requestFocus(View.FOCUS_DOWN);

		WebSettings webSettings = appView.getSettings();
		webSettings.setJavaScriptEnabled(true);
		webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
		webSettings.setDomStorageEnabled(true);
		webSettings.setSaveFormData(false);
		webSettings.setSavePassword(false);
		webSettings.setLoadsImagesAutomatically(true);
		webSettings.setBlockNetworkImage(false);
		webSettings.setGeolocationEnabled(true);
		appView.setWebViewClient(webViewClientExt);
		appView.postUrl(Tools.getURL(Constant.GET_PAYBYCREDITCARD,null ), creditCardPaymentPostMethod(dealID,sessionID,Integer.parseInt(quantitypassed)));
		
		
	}
	private class WebViewClientExt extends WebViewClient {
		
		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			super.onPageStarted(view, url, favicon);
			
		}
		

		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			return true;
		}

		@Override
		public void onPageFinished(WebView view, String url) {
			((ProgressBar) findViewById(R.id.progress)).setVisibility(View.INVISIBLE);
			
		}
	}


	
	
	public byte[] creditCardPaymentPostMethod(String dealid, String sessionid, int quantity){
		String postData ="id_deal="+dealid+"&sessionToken="+sessionid+"&quantity="+quantity;
		return EncodingUtils.getBytes(postData, "BASE64");
	}
	
	

	private void gotoMyCoupons(){
		ApplicationEx.getInstance().currentTab = 1;
		 getParent().onBackPressed();
	}


	public void setConfirmAlert(){
		AlertDialog.Builder builder = new AlertDialog.Builder(this.getParent());
		
			builder.setMessage("Do you want to go to your Coupons?")
				   .setCancelable(false)
			       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
			           public void onClick(DialogInterface dialog, int id) {
			        	   dialog.cancel(); 
			        	   dialog.dismiss();
			        	   	ApplicationEx.getInstance().isConfirm = true;
			        	//TODO go to my coupons  
			        	   	gotoMyCoupons();
			           }
			       })
			       .setNegativeButton("No", new DialogInterface.OnClickListener() {
			           public void onClick(DialogInterface dialog, int id) {
			                dialog.cancel();
			                ApplicationEx.getInstance().isConfirm = false;
			                getParent().onBackPressed();
			           }
			       });
			AlertDialog alert = builder.create();
			alert.show();
			
		}
   

	@Override
	public void onClick(View v) {
		
	if (v.getId() == R.id.header_iconright){
			setConfirmAlert();
			
		}
	
	}

	
	public BroadcastReceiver activeDealsBRoadcastReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {

		}
	};
	
	@Override
	public void onResume() {
		
		super.onResume();
		getPaybyCreditcard();
	}
	


	
}
